package com.purong.purong.fragment;

import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.purong.purong.R;
import com.purong.purong.adapter.LoanPageAdapter;
import com.purong.purong.util.CommonUtil;

import java.util.ArrayList;

/**
 * Created by wenjiezeng on 2017/11/10.
 */
public class LoanFragment extends BaseFragment {
    private ImageView statusBar;
    private ViewPager mViewPager;
    private LoanPageAdapter mAdapter;
    private SlidingTabLayout tabLayout;
//    private TextView quickTv,bankTv;
//    private ImageView quickImg,bankImg;
    private ArrayList<Fragment> fragmentList=new ArrayList<>();
    private ArrayList<String> titleList = new ArrayList<>();
    private int selectPosition=0;//当前fragment位置

    @Override
    public void initView(View rootView){
        statusBar = (ImageView) rootView.findViewById(R.id.status_bar);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
            statusBar.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
        }else {
            statusBar.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, CommonUtil.getStatusBarHeight(getActivity())));
        }
        tabLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_tablayout);
        mViewPager=(ViewPager)rootView.findViewById(R.id.view_pager);
//        quickTv=(TextView)rootView.findViewById(R.id.quick_load_title);
//        bankTv=(TextView)rootView.findViewById(R.id.bank_loan_title);

//        quickImg=(ImageView) rootView.findViewById(R.id.quick_load_tag);
//        bankImg=(ImageView) rootView.findViewById(R.id.bank_loan_tag);
        SmallLoanFragment speedFragment=new SmallLoanFragment();
        fragmentList.add(speedFragment);
        BankLoanFragment bankFragment=new BankLoanFragment();
        fragmentList.add(bankFragment);
        titleList.add("极速贷");
        titleList.add("银行贷");
        mAdapter=new LoanPageAdapter(getChildFragmentManager(),fragmentList);
        mAdapter.setPageTitle(titleList);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                if (position==0){
//                    quickTv.setTextColor(getResources().getColor(R.color.text_dark_gray));
//                    bankTv.setTextColor(getResources().getColor(R.color.text_gray));
//                    quickImg.setVisibility(View.VISIBLE);
//                    bankImg.setVisibility(View.INVISIBLE);
//                }else if (position==1){
//                    quickTv.setTextColor(getResources().getColor(R.color.text_gray));
//                    bankTv.setTextColor(getResources().getColor(R.color.text_dark_gray));
//                    quickImg.setVisibility(View.INVISIBLE);
//                    bankImg.setVisibility(View.VISIBLE);
//                }
                selectPosition=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabLayout.setViewPager(mViewPager);
        tabLayout.setTabSpaceEqual(false);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {

            }

            @Override
            public void onTabReselect(int position) {


            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public int getRootViewId() {
        return R.layout.fragment_loan;
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.quick_lay:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.bank_lay:
                mViewPager.setCurrentItem(1);
                break;
        }
    }

    public void scroll2Top(){
        if (fragmentList!=null&&fragmentList.size()>=selectPosition+1){
            if (selectPosition==0){
                ((SmallLoanFragment)fragmentList.get(selectPosition)).scroll2Top();

            }else if (selectPosition==1){
                ((BankLoanFragment)fragmentList.get(selectPosition)).scroll2Top();

            }
        }
    }
}
